package com.pw.demo3.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.ibatis.annotations.Mapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean ShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);

        //添加内置过滤器
        Map<String, String> filterMap = new LinkedHashMap<>();
//        filterMap.put("/department/*", "perms[department:list]");
//        filterMap.put("/employee/*", "perms[employee:list]");
//        filterMap.put("/permission/*", "authc");
//        filterMap.put("/role/*", "authc");

        filterMap.put("/department/list", "perms[department:list]");
        filterMap.put("/department/input", "perms[department:input]");
        filterMap.put("/department/delete", "perms[department:delete]");

        filterMap.put("/employee/list", "perms[employee:list]");
        filterMap.put("/employee/input", "perms[employee:input]");
        filterMap.put("/employee/delete", "perms[employee:delete]");

        filterMap.put("/permission/list", "perms[employee:list]");
        filterMap.put("/permission/input", "perms[employee:input]");
        filterMap.put("/permission/delete", "perms[employee:delete]");

        filterMap.put("/role/list", "perms[role:list]");
        filterMap.put("/role/input", "perms[role:input]");
        filterMap.put("/role/delete", "perms[role:delete]");


        bean.setFilterChainDefinitionMap(filterMap);
        //没有权限
        bean.setLoginUrl("/");
        //未授权
        bean.setUnauthorizedUrl("/noPermission");
        //错误

        return bean;
    }

    //DefaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建realm:1
    @Bean
    public UserRealm userRealm(@Qualifier("hashedCredentialsMatcher") HashedCredentialsMatcher matcher) {

//        //获取当前用户
//        Subject subject=SecurityUtils.getSubject();
//
//        //封装用户登录数据
//        new UsernamePasswordToken()
        UserRealm userRealm = new UserRealm();
        userRealm.setAuthorizationCachingEnabled(false);
        userRealm.setCredentialsMatcher(matcher);
        return userRealm;
    }

    //md5加密
    @Bean("hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //指定加密方式为MD5
        credentialsMatcher.setHashAlgorithmName("MD5");
        //加密次数
        credentialsMatcher.setHashIterations(1);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }

    //整合thymeleaf
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
}